﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- 6. Write a function that checks if the element at given 
			position in given array of integers is bigger than its 
			two neighbors (when such exist).  -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		function isBiggerThanNeighbors(position, array) {
			var result = 0;
			
			if (position <= 0 || position >= (array.length - 1)) {
				result = -1;
			}
			else {
				if(array[position] > array[position + 1] &&
					array[position] > array[position - 1]) {
					result = 1;
				}
			}

			return result;
		}

		// Process user input
		var search = parseInt(prompt("Enter element index to check"));
		var numbers = new Array();

		if (!isNaN(search)) {
			var input = prompt("Enter numbers separated by spaces");
			var array = input.split(' ');

			// Collect numbers only
			var j = 0;
			for (var i in array) {
				var num = parseInt(array[i]);
				if (!isNaN(num)) {
					numbers[j] = num;
					j++;
				}
			}
		}

		// If there are numbers entered
		if (numbers.length > 0) {
			jsConsole.write("Element index: ");
			jsConsole.writeLine(search);
			jsConsole.write("All numbers: ");
			jsConsole.writeLine(numbers.join());

			// Check if bigger
			var result = isBiggerThanNeighbors(search, numbers);
			var output = "Insufficient number of neighbors."
			if (result >= 0) {
				output = "Element at position " + search +
					" in the array is bigger than its two neighbors = "+
					(result == 1 ? true : false);
				}
			jsConsole.writeLine(output);
		}
		else {
			jsConsole.writeLine("Wrong input");
		}
	</script>
</body>
</html>
